package com.jbl.te.controller;

import com.jbl.commonutils.JwtUtils;
import com.jbl.commonutils.R;
import com.jbl.te.entity.User;
import com.jbl.te.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R Login(@RequestBody User user) {

        String token = userService.login(user);
        return R.ok().data("token",token);
    }

    @GetMapping("/info")
    public R getInfo(String token) {
        if(!JwtUtils.checkToken(token)) {
            return R.error().message("token错误");
        }
        String id = JwtUtils.getMemberIdByJwtToken(token);
        User user = userService.getById(id);
        List<String> roleList = new ArrayList<>();
        roleList.add(user.getRole());
        return R.ok().data("id",user.getId())
                .data("roles",roleList)
                .data("introduction",user.getIntroduction())
                .data("avatar",user.getAvatar())
                .data("name",user.getName());
    }

    @PostMapping("/logout")
    public R logout() {
        return R.ok().data("data","success");
    }


}
